using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Authentication;
using Kingdee.BOS.ServiceFacade.KDServiceClient.Account;
using Kingdee.BOS.ServiceFacade.KDServiceClient.User;
using Kingdee.BOS.ServiceHelper;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace K3DesktopClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string host = "http://192.168.0.19";
        public Context ctx { get; private set; }

        private void okBtn_Click(object sender, System.EventArgs e)
        {
            AccountClientProxy accountClientProxy = new AccountClientProxy();
            accountClientProxy.HostURL = host + "/k3cloud/";
            string userName = userTxt.Text.Trim();
            string userPwd = passTxt.Text.Trim();
            int lcid = 2052;
            string acctID = "6115be3f79b045";
            var loginInfo = new LoginInfo()
            {
                Username = userName,
                Password = userPwd,
                AcctID = acctID,
                Lcid = lcid,
                ValidationCode = veryTxt.Text,
                AuthenticateType = AuthenticationType.PwdAuthentication,
            };
            UserServiceProxy userServiceProxy = new UserServiceProxy();
            userServiceProxy.HostURL = host + "/k3cloud/";
            var cc = userServiceProxy.ValidateUser("", loginInfo);
            ctx = cc.Context;
            this.Text = ctx.UserName;
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            var sql = "SELECT FUSERID,FNAME,FUSERTYPE,FFORBIDSTATUS FROM T_SEC_USER";
            var dt = DBServiceHelper.ExecuteScalar(ctx, sql, "");
            Console.WriteLine(dt);
            //var res = DBUtils.Execute(ctx, sql);
            //Console.WriteLine(res);
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            veryPic_Click(sender, e);
        }

        private void veryPic_Click(object sender, EventArgs e)
        {
            var imgUrl = $"{host}/k3cloud/Kingdee.BOS.ServiceFacade.ServicesStub.Account.AccountService.GetValidationCodeImageByte.common.kdsvc";
            try
            {
                AccountClientProxy accountClientProxy = new AccountClientProxy();
                accountClientProxy.HostURL = host + "/k3cloud/";
                byte[] validationCodeImageByte = accountClientProxy.GetValidationCodeImageByte();
                if (validationCodeImageByte != null)
                {
                    using (MemoryStream memoryStream = new MemoryStream(validationCodeImageByte))
                    {
                        memoryStream.Position = 0;
                        var bmp = (Bitmap)Image.FromStream(memoryStream);
                        veryPic.Image = bmp;
                        memoryStream.Close();
                    }
                }
            }
            catch
            {

            }
        }
    }
}